--*************ANIMATION SUITE****************--
--**************Property of Hxrmn*****************--
--*************Hxrmn@comcast.net*****************--
--DO NOT RE-DISTRIBUTE WITHOUT HXRMN'S CONSENT--
--********************************************--

PANEL = {}


surface.CreateFont( "Tahoma", 20,  700, false, false, "TrackFont2" )
surface.CreateFont( "Tahoma", 12,  700, false, false, "MiniHelpFont" )

function PANEL:ColorBrightness(color,amt)
	local nc = Color(math.Clamp(color.r+amt,0,255)
			,math.Clamp(color.g+amt,0,255)
			,math.Clamp(color.b+amt,0,255)
			,color.a)
	return nc
end

function PANEL:Init()
	self:SetSize(self:GetWide(), 35)
	self.length = 0
	self.currframe = 0
	self.btnClose:Remove()
	self.btnClose = nil
	self.lblTitle:Remove()
	self.lblTitle = nil
	self.MouseDown = false
	self.MousePos_X = 0
	self.MousePos_Y = 0
end

function PANEL:OnMousePressed()
	self.MouseDown = true
end

function PANEL:OnMouseReleased()
	self.MouseDown = false
end

function PANEL:GetRelativeMousePos()
	x1, y1 = gui.MousePos() 
	x2, y2 = self:GetPos()
	x3, y3 = self:GetParent():GetPos()
	
	x2 = x2 + x3
	x1 = x1 - x2
	
	y2 = y2 + y3
	y1 = y1 - y2
	return x1, y1
end

function PANEL:InBounds(x,y)
	is = true
	if(x < 0) then is = false end
	if(y < 0) then is = false end
	if(x > self:GetWide()) then is = false end
	if(y > self:GetTall()) then is = false end
	return is
end

function PANEL:ToAbsolute(x,y)
	x2, y2 = self:GetPos()
	x3, y3 = self:GetParent():GetPos()
	
	x2 = x2 + x3
	x = x + x2
	
	y2 = y2 + y3
	y = y + y2
	return x,y
end

function PANEL:ToRelative(x,y)
	x2, y2 = self:GetPos()
	x3, y3 = self:GetParent():GetPos()
	
	x2 = x2 + x3
	x = x - x2
	
	y2 = y2 + y3
	y = y - y2
	return x,y
end

function PANEL:MouseMoved(x,y)
	roundup = false
	
	if(self.MouseDown) then
		x = x / self:GetWide()
		if(x > 0.5) then roundup = true end
		local cnt = self.length*x
		if(roundup) then 
			cnt = math.ceil(cnt)
		else
			cnt = math.floor(cnt)
		end
		cnt = math.Clamp(cnt,0,self.length)
		if(cnt != AS_GetAnimator():GetTime()) then
			AS_GetAnimator():GetControls():GotoTime(cnt)
			self.currframe = cnt
		end
		--Msg(x .. " " .. cnt .. " " .. self.length .. "\n")
	end
end

function PANEL:ConstrainMouse()
	x, y = self:GetRelativeMousePos()
	mx,my = gui.MousePos()
	xm,ym = self:ToAbsolute(self:GetWide(),self:GetTall())
	if(x > self:GetWide()) then
		gui.SetMousePos(xm,my)
	end
	if(y > self:GetTall()) then
		gui.SetMousePos(mx,ym)
	end
	xm,ym = self:ToAbsolute(0,0)
	if(x < 0) then
		gui.SetMousePos(xm,my)
	end
	if(y < 0) then
		gui.SetMousePos(mx,ym)
	end
end

function PANEL:Think()
	--Msg(xp .. " " .. yp .. "\n")
	self.length = AS_GetAnimator():GetTimelineLength()
	if(self.currframe > self.length) then
		self.currframe = self.currframe - 5
		if(self.currframe < 0) then self.currframe = 0 end
	end
	x, y = self:GetRelativeMousePos()
	
	if(!self:InBounds(x,y)) then
		if(self.MouseDown) then
			--self:ConstrainMouse()
			if(!input.IsMouseDown()) then
				self:OnMouseReleased()
			end
		end
	end
	if(x != self.MousePos_X or y != self.MousePos_Y) then
		self.MousePos_X = x
		self.MousePox_Y = y
		self:MouseMoved(x,y)
	end
end

function PANEL:Paint()
	local color_text = Color( 255, 255, 255, 255 )
	local color_box  = Color( 100,100,100,255 )
		
		surface.SetDrawColor(color_box.r,color_box.g,color_box.b,color_box.a)
		surface.DrawRect(0,0,self:GetWide(),self:GetTall())
		
		
		color_box = self:ColorBrightness(color_box,-20)
		
		surface.SetDrawColor(color_box.r,color_box.g,color_box.b,color_box.a)
		surface.DrawOutlinedRect(0,0,self:GetWide(),self:GetTall())
		
		surface.SetDrawColor(color_text.r,color_text.g,color_text.b,color_text.a/2)
		local percent = (self.currframe / self.length)*self:GetWide()
		draw.DrawText( self.currframe .. "/" .. self.length, "TrackFont2", self:GetWide()/2, 8, color_text, TEXT_ALIGN_CENTER )
		surface.DrawRect(percent-2,0,4,self:GetTall())

		color_text.a = color_text.a/2
		draw.DrawText( "Drag to scroll through frames.", "MiniHelpFont", 10, 8, color_text, TEXT_ALIGN_LEFT )
	return true
		
end

function PANEL:ResetFrameCount()
	if(self.currframe > self.length) then
		self.currframe = self.length
	end
end

function PANEL:ClearFrame(t)
	if(self.frames[t] == nil) then
		self.frames[t]:Remove()
		self.frames[t] = nil
	end
end


vgui.Register("vTimeline", PANEL, "DFrame")